﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;

namespace Marmat.Forms.Views
{
    namespace Grid
    {
        [Editor(typeof(Band_Form_Designer), typeof(UITypeEditor))]
        public class BandCollection : IList
        {
            internal BaseGrid _grid;
            private List<Band> list = new List<Band>();
            internal DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection _bands;

            internal BandCollection(BaseGrid Grid, DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection Bands)
            {
                this._grid = Grid;
                this._bands = Bands;
            }

            protected BaseGrid Grid => this._grid;

            private DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection Bands
            {
                get => this._bands;
            }

            void ICollection.CopyTo(Array array, int index)
            {
            }

            /// <summary>
            /// 获取 System.Collections.ICollection 中包含的元素数。
            /// </summary>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            public int Count
            {
                get => this.list.Count;
            }

            bool ICollection.IsSynchronized => true;

            object ICollection.SyncRoot => this;

            public IEnumerator GetEnumerator()
            {
                return this.list.GetEnumerator();
            }

            /// <summary>
            /// 向 System.Collections.IList 中添加项。
            /// </summary>
            /// <param name="Item"></param>
            /// <remarks></remarks>
            public void Add(Band Item)
            {
                IList list = this;
                list.Add(Item);
            }

            /// <summary>
            /// 向 System.Collections.IList 中添加项。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            int IList.Add(object value)
            {
                Band Item = (Band)value;
                Item._grid = this._grid;
                if (this.Bands is object)
                {
                    this.Bands.Add(Item._band).Visible = true;
                }

                this.list.Add(Item);
                return -1;
            }

            /// <summary>
            /// 重新构建表头
            /// </summary>
            public void Build()
            {
                this.Grid.BuildHeader("_");
            }

            public void Clear()
            {
                this.Bands?.Clear();
                this.list.Clear();
            }

            /// <summary>
            /// 确定 System.Collections.IList 是否包含特定值。
            /// </summary>
            /// <param name="Item"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public bool Contains(Band Item)
            {
                return this.Contains(Item);
            }

            /// <summary>
            /// 确定 System.Collections.IList 是否包含特定值。
            /// </summary>
            /// <param name="itemName"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public bool Contains(string itemName)
            {
                return this.list.Exists(x => x.Name == itemName);                
            }

            /// <summary>
            /// 确定 System.Collections.IList 是否包含特定值。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            bool IList.Contains(object value)
            {
                return this.list.Contains((Band)value);
            }


            /// <summary>
            /// 确定 System.Collections.IList 中特定项的索引。
            /// </summary>
            /// <param name="Item"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            public int IndexOf(Band Item)
            {
                return this.IndexOf(Item);
            }

            /// <summary>
            /// 确定 System.Collections.IList 中特定项的索引。
            /// </summary>
            /// <param name="value"></param>
            /// <returns></returns>
            /// <remarks></remarks>
            int IList.IndexOf(object value)
            {
                return this.list.IndexOf((Band)value);
            }

            /// <summary>
            /// 在 System.Collections.IList 中的指定索引处插入项。
            /// </summary>
            /// <param name="index"></param>
            /// <param name="value"></param>
            /// <remarks></remarks>
            public void Insert(int index, Band value)
            {
                this.Insert(index, value);
            }

            /// <summary>
            /// 在 System.Collections.IList 中的指定索引处插入项。
            /// </summary>
            /// <param name="index"></param>
            /// <param name="value"></param>
            /// <remarks></remarks>
            void IList.Insert(int index, object value)
            {
                if (this.Bands is object)
                {
                    this.Bands.Insert(index, (DevExpress.XtraGrid.Views.BandedGrid.GridBand)value);
                }

                this.list.Insert(index, (Band)value);
            }

            bool IList.IsFixedSize
            {
                get
                {
                    return false;
                }
            }

            bool IList.IsReadOnly
            {
                get
                {
                    return false;
                }
            }

            /// <summary>
            /// 获取指定索引处的元素。
            /// </summary>
            /// <param name="index"></param>
            /// <value></value>
            /// <returns></returns>
            /// <remarks></remarks>
            public Band this[int index]
            {
                get
                {
                    return (Band)this.list[index];
                }
            }

            object IList.this[int index]
            {
                get => this.list[index];
                set => throw new NotImplementedException();
            }


            /// <summary>
            /// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
            /// </summary>
            /// <param name="Item"></param>
            /// <remarks></remarks>
            public void Remove(Band Item)
            {
                this.Remove(Item);
            }

            /// <summary>
            /// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
            /// </summary>
            /// <param name="ItemName"></param>
            /// <remarks></remarks>
            public void Remove(string ItemName)
            {
                foreach (Band Item in this.list)
                {
                    if ((Item.Name ?? "") == (ItemName ?? ""))
                    {
                        this.Remove(Item);
                        break;
                    }
                }
            }

            /// <summary>
            /// 从 System.Collections.IList 中移除特定对象的第一个匹配项。
            /// </summary>
            /// <param name="value"></param>
            /// <remarks></remarks>
            void IList.Remove(object value)
            {
                Band item = (Band)value;
                if (this.Bands is object)
                {
                    this.Bands.Remove(item._band);
                }

                this.list.Remove(item);
            }

            /// <summary>
            /// 移除指定索引处的 System.Collections.IList 项。
            /// </summary>
            /// <param name="index"></param>
            /// <remarks></remarks>
            public void RemoveAt(int index)
            {
                this.list.RemoveAt(index);
                if (this.Bands is object)
                {
                    this.Bands.RemoveAt(index);
                }
            }
        }
    }
}